package cn.xinfei.xdecision.engine.runner.metadata;

import cn.xinfei.xdecision.common.model.datax.datainterface.v2.VariableVO;
import cn.xinfei.xdecision.common.model.datax.datamanage.Field;
import cn.xinfei.xdecision.common.model.datax.enums.VirtualDataSourceEnum;
import cn.xinfei.xdecision.common.service.datax.datamanage.FieldService;
import cn.xinfei.xdecision.common.service.datax.datamanage.v2.VariableService;
import cn.xinfei.xdecision.common.vo.FieldInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class FieldInfoLoader {

    @Resource
    private FieldService fieldService;

    @Resource
    private VariableService variableService;

    public Map<String, FieldInfoVo> init(String fieldCode) {
        log.info("FieldInfoLoader start");


        Map<String, FieldInfoVo> fieldMap = new HashMap<>();
        int pageNum = 0;
        int pageSize = 100;
        long id = 0;
        int inc = 0;
        while (true) {
            PageMethod.startPage(pageNum, pageSize);
            LambdaQueryWrapper<Field> fieldWrapper = new LambdaQueryWrapper<>();
            if (StringUtils.isNotEmpty(fieldCode)) {
                fieldWrapper.eq(Field::getFieldCode, fieldCode);
            }
            fieldWrapper.eq(Field::getStatus, 1);
            fieldWrapper.eq(Field::getIsDeleted, "N");
            fieldWrapper.ge(Field::getId, id);
            fieldWrapper.orderByAsc(Field::getId);
            long start = System.currentTimeMillis();
            List<Field> list = fieldService.list(fieldWrapper);
            PageInfo<Field> pageInfo = new PageInfo<>(list);

            long end = System.currentTimeMillis();
            log.info("fieldService.list duration={}", end - start);
            Set<String> fieldCodes = list.stream().map(Field::getFieldCode).collect(Collectors.toSet());
            Collection<VariableVO> variableVOS = variableService.findVarTypeByVarCodes(fieldCodes);
            long step2 = System.currentTimeMillis();
            log.info("variableService.list duration={}", step2 - end);
            Map<String, VariableVO> variableTypeMap = variableVOS.stream().collect(Collectors.toMap(VariableVO::getVarCode, v -> v, (k1, k2) -> k1));

            for (Field field : list) {
                Long fieldId = field.getId();
                if (fieldId >= id) {
                    id = fieldId;
                }
                FieldInfoVo fieldInfoVo = new FieldInfoVo();
                BeanUtils.copyProperties(field, fieldInfoVo);
                String fieldCode1 = field.getFieldCode();
                VariableVO variableVO = variableTypeMap.get(fieldCode1);
                if (null != variableVO) {
                    //变量类型 java(J),python(P),model(M),credit(C),standard(S)
                    fieldInfoVo.setSourceType(VirtualDataSourceEnum.CREDIT_VAR.getCode());
                }
                fieldMap.put(fieldCode1, fieldInfoVo);
            }
            log.info("FieldInfoLoader page.size={},id={},pageNum={},incr={}", list.size(), id, pageNum, inc++);
            int nextPage = pageInfo.getNextPage();
            if (nextPage >= pageInfo.getPages()) {
                log.info("FieldInfoLoader end.size={}", fieldMap.size());
                break;
            }

        }

        return fieldMap;
    }
}
